package com.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.server.CountService;
import com.server.InfoService;

public class Count {
	
	 //1.用户总数、商家总数、交易总次数、交易总积分数、日交易次数、日交易积分数
	 //用户总数
	 public String UsersCount(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String countSql="select count(id) from user";
		  CountService countService=new CountService();
		  int usersCount=countService.getCounts(countSql);
		  request.setAttribute("usersCount",usersCount);
	      return "success"; 
	    }
	 //商家总数
	 public String ShopCount(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String countSql="select count(id) from shopinfo";
		  CountService countService=new CountService();
		  int shopCount=countService.getCounts(countSql);
		  request.setAttribute("shopCount",shopCount);
	      return "success"; 
	    } 
	 //交易总次数
	 public String OrderCount(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String countSql="select ordersum from shopinfo";
		  CountService countService=new CountService();
		  int orderCount=countService.getAllCounts(countSql);
		  request.setAttribute("orderCount",orderCount);
	      return "success"; 
	    } 
	 //交易总积分数
	 public String CreditCount(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String countSql="select credit from shopinfo";
		  CountService countService=new CountService();
		  int creditCount=countService.getAllCounts(countSql);
		  request.setAttribute("creditCount",creditCount);
	      return "success"; 
	    } 
	 //日交易积分数
	 public String CreditCountByDay(){
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		  String countSql="select credit from orderitem where date(confirmtime)='"+dateStr+"'";
		  System.out.println(countSql);
		  CountService countService=new CountService();
		  int creditCountByDay=countService.getAllCounts(countSql);
		  request.setAttribute("creditCountByDay",creditCountByDay);
	      return "success"; 
	    } 
	 //日交易次数
	 public String OrderCountByDay(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		  String countSql="select count(id) from orderitem where date(confirmtime)='"+dateStr+"'";
		  CountService countService=new CountService();
		  int orderCountByDay=countService.getCounts(countSql);
		  request.setAttribute("orderCountByDay",orderCountByDay);
	      return "success"; 
	    } 
	 
	 
	 
	 
	 //2.按类别查看，商家总数、交易总次数、交易总积分数、日交易次数、日交易积分数
	 //商家总数
	 public String ShopCountByTytpe(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  String countSql;
		  if(request.getParameter("type")!=null&&Integer.valueOf(request.getParameter("type"))!=-1)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
			  countSql="select count(s.id) from shopinfo s where s.type="+type;
		  }		 
		  else {
			  countSql="select count(s.id) from shopinfo s";
		  }
		  CountService countService=new CountService();
		  int shopCountByTytpe=countService.getCounts(countSql);
		  request.setAttribute("shopCountByTytpe",shopCountByTytpe);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map typeM=serviceTypeService.getType();
		  request.setAttribute("typeS", typeM);
	      return "success"; 
	    } 
	 //交易总次数
	 public String OrderCountByType(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  String countSql;
		  if(request.getParameter("type")!=null&&Integer.valueOf(request.getParameter("type"))!=-1)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
			  countSql="select s.ordersum from shopinfo s inner join paservice p on p.shopid=s.id where p.type="+type;
		  }else {
			  countSql="select s.ordersum from shopinfo s inner join paservice p on p.shopid=s.id";
		  }	
		 
		  CountService countService=new CountService();
		  int orderCountByType=countService.getAllCounts(countSql);
		  request.setAttribute("orderCountByType",orderCountByType);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map typeM=serviceTypeService.getType();
		  request.setAttribute("typeS", typeM);
	      return "success"; 
	    } 
	 //交易总积分数
	 public String CreditCountByType(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  String countSql;
		  if(request.getParameter("type")!=null&&Integer.valueOf(request.getParameter("type"))!=-1)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
			  countSql="select s.credit from shopinfo s inner join paservice p on p.shopid=s.id where p.type="+type;
		  }else {
			  countSql="select s.credit from shopinfo s inner join paservice p on p.shopid=s.id";
		  }	
		  
		  CountService countService=new CountService();
		  int creditCountByType=countService.getAllCounts(countSql);
		  request.setAttribute("creditCountByType",creditCountByType);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map typeM=serviceTypeService.getType();
		  request.setAttribute("typeS", typeM);
	      return "success"; 
	    } 
	 //日交易积分数
	 public String CreditCountByDayAndType(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		  String countSql;
		  if(request.getParameter("type")!=null&&Integer.valueOf(request.getParameter("type"))!=-1)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
			  if(dateStr!=null&&dateStr!="")
			  {
				  countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id where type="+type+" and date(confirmtime)='"+dateStr+"'";
			  }
			  else {
				  countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id where type="+type;
			  }
			  
		  }else {
			  if(dateStr!=null&&dateStr!="")
			  {
				  countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id where date(confirmtime)='"+dateStr+"'";
			  }
			  else {
				  countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id";
			  }
			  
		  }
		  
		  
		  CountService countService=new CountService();
		  int creditCountByDayAndType=countService.getAllCounts(countSql);
		  request.setAttribute("creditCountByDayAndType",creditCountByDayAndType);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map typeM=serviceTypeService.getType();
		  request.setAttribute("typeS", typeM);
	      return "success"; 
	    } 
	 //日交易次数
	 public String OrderCountByDayAndType(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		  String countSql;
		  if(request.getParameter("type")!=null&&Integer.valueOf(request.getParameter("type"))!=-1)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
			  if(dateStr!=null&&dateStr!="")
			  {
				  countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id where type="+type+" and date(confirmtime)='"+dateStr+"'";
			  }
			  else {
				  countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id where type="+type;
			  }
		  }else {
			  if(dateStr!=null&&dateStr!="")
			  {
				  countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id where date(confirmtime)='"+dateStr+"'";
			  }
			  else {
				  countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id";
			  }
			  
		  }
		  CountService countService=new CountService();
		  int orderCountByDayAndType=countService.getCounts(countSql);
		  request.setAttribute("orderCountByDayAndType",orderCountByDayAndType);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map typeM=serviceTypeService.getType();
		  request.setAttribute("typeS", typeM);
	      return "success"; 
	    } 
	 
	 
	 
	 
	 //4.按商家查看，交易总次数、交易总积分数、日交易次数、日交易积分数、评价总得分、评价详情
	 
	 //按商家查看交易总次数
	 public String OrderCountByShop(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		 //获取商家
		 int shopId=-1;
		 String countSql;
		 if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1)
		 {
			 shopId=Integer.valueOf(request.getParameter("shopId"));
			 countSql="select ordersum from shopinfo where id="+shopId;
		 }else {
			 countSql="select ordersum from shopinfo";
		 }
		 CountService countService=new CountService();
		 int orderCountByShop=countService.getCounts(countSql);
		 request.setAttribute("orderCountByShop",orderCountByShop);
		 //下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map shopMap=serviceTypeService.getShop();
		 request.setAttribute("shop", shopMap);
		 return "success";
	 }
	//按商家查看交易总积分数
	 public String CreditCountByShop(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		 //获取商家
		//获取商家
		 int shopId=-1;
		 String countSql;
		 if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1)
		 {
			 shopId=Integer.valueOf(request.getParameter("shopId"));
			 countSql="select credit from shopinfo where id="+shopId;
		 }else {
			 countSql="select credit from shopinfo";
		}
		 CountService countService=new CountService();
		 int creditCountByShop=countService.getAllCounts(countSql);
		 request.setAttribute("creditCountByShop",creditCountByShop);
		//下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map shopMap=serviceTypeService.getShop();
		 request.setAttribute("shop", shopMap);
		 return "success";
	 }
	//按商家查看日交易积分数
	 public String CreditCountByShopAndDay(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		//获取商家
		 int shopId=-1;
		 String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		 String countSql;
		 if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1)
		 {
			 shopId=Integer.valueOf(request.getParameter("shopId"));
			 countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where si.id="+shopId;
			 if(dateStr!=null&&dateStr!="")
			 {
				 countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where si.id="+shopId+" and date(confirmtime)='"+dateStr+"'";
			 }
			 
		 }else {
			 countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id";
			 if(dateStr!=null&&dateStr!="")
			 {
				 countSql="select o.credit from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where date(confirmtime)='"+dateStr+"'";
			 }
		 }
		 
		 
		 CountService countService=new CountService();
		 int creditCountByShopAndDay=countService.getAllCounts(countSql);
		 request.setAttribute("creditCountByShopAndDay",creditCountByShopAndDay);
		//下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map shopMap=serviceTypeService.getShop();
		 request.setAttribute("shop", shopMap);
		 return "success";
	 }
	//按商家查看日交易次数
	 public String OrderCountByShopAndDay(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取商家
		  int shopId=-1;
		  String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		  String countSql;
		  if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1)
			 {
				 shopId=Integer.valueOf(request.getParameter("shopId"));
				 countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where si.id="+shopId;
				 if(dateStr!=null&&dateStr!="")
				 {
					 countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where si.id="+shopId+" and date(confirmtime)='"+dateStr+"'";
				 }
				 
			 }else {
				 countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id";
				 if(dateStr!=null&&dateStr!="")
				 {
					 countSql="select count(o.id) from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where date(confirmtime)='"+dateStr+"'";
				 }
			 }
		  
		  
		  CountService countService=new CountService();
		  int orderCountByShopAndDay=countService.getCounts(countSql);
		  request.setAttribute("orderCountByShopAndDay",orderCountByShopAndDay);
		  //下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map shopMap=serviceTypeService.getShop();
		 request.setAttribute("shop", shopMap);
	      return "success"; 
	    } 
	//按商家查看总评价
	 public String StarsumByShop(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取商家
		  int shopId=-1;
		  String countSql;
		  if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1)
		  {
			  shopId=Integer.valueOf(request.getParameter("shopId"));
			  countSql="select starsum from shopinfo where id="+shopId;
		  }else {
			  countSql="select starsum from shopinfo";
		  }
		  CountService countService=new CountService();
		  int starsumByShop=countService.getCounts(countSql);
		  request.setAttribute("starsumByShop",starsumByShop);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map shopMap=serviceTypeService.getShop();
		  request.setAttribute("shop", shopMap);
	      return "success"; 
	    } 
	//按商家查看总评价详情
	 public String CommentByShop(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取商家
		  int shopId=-1;
		  String countSql; 
		  if(request.getParameter("shopId")!=null&&Integer.valueOf(request.getParameter("shopId"))!=-1){
			  shopId=Integer.valueOf(request.getParameter("shopId"));
			  countSql="select o.comment from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id where si.id="+shopId;
		  }else {
			  countSql="select o.comment from orderitem o inner join paservice p on o.paserviceid=p.id inner join shopinfo si on p.shopid=si.id";
		  }
		  CountService countService=new CountService();
		  List commentByShop=countService.getAllValues(countSql);
		  request.setAttribute("commentByShop",commentByShop);
		  //下拉列表的值
		  InfoService serviceTypeService=new InfoService();
		  Map shopMap=serviceTypeService.getShop();
		  request.setAttribute("shop", shopMap);
	      return "success"; 
	    } 
	 
	 
	 
	 
	 //6.按用户查看，交易总次数、交易总积分数、日交易次数、日交易积分数
	 //按用户查看交易总次数
	 public String CountByUser(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		 //获取用户
		 int userId=-1;
		 String countSql;
		 if(request.getParameter("userId")!=null&&Integer.valueOf(request.getParameter("userId"))!=-1)
		 {   
			 userId=Integer.valueOf(request.getParameter("userId"));
			 countSql="select count(id) from orderitem where userid="+userId+" and confirmtime>0";
		 }else {
			 countSql="select count(id) from orderitem where confirmtime>0";
		 }
		 CountService countService=new CountService();
		 int countByUser=countService.getCounts(countSql);
		 request.setAttribute("countByUser",countByUser);
		 //下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map userM=serviceTypeService.getUser();
		 request.setAttribute("user", userM);
		 return "success";
	 }
	//按用户查看交易总积分数
	 public String CreditByUser(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		//获取用户
		 int userId=-1;
		 String countSql;
		 if(request.getParameter("userId")!=null&&Integer.valueOf(request.getParameter("userId"))!=-1)
		 {   
			 userId=Integer.valueOf(request.getParameter("userId"));
			 countSql="select credit from orderitem where userid="+userId;
		 }else {
			 countSql="select credit from orderitem";
		 }
		 
		 CountService countService=new CountService();
		 int creditByUser=countService.getAllCounts(countSql);
		 request.setAttribute("creditByUser",creditByUser);
		 //下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map userM=serviceTypeService.getUser();
		 request.setAttribute("user", userM);
		 return "success";
	 }
	 //按用户查看日交易总积分数
	 public String CreditByUserAndDay(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		 //获取用户
		 int userId=-1;
		 String countSql;
		 String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		 if(request.getParameter("userId")!=null&&Integer.valueOf(request.getParameter("userId"))!=-1)
		 {   
			 userId=Integer.valueOf(request.getParameter("userId"));
			 countSql="select credit from orderitem where userid="+userId;
			 if(dateStr!=null&&dateStr!="")
			 {
				 countSql="select credit from orderitem where userid="+userId+" and date(confirmtime)='"+dateStr+"'";
			 }
			 
		 }else {
			 countSql="select credit from orderitem";
			 if(dateStr!=null&&dateStr!="")
			 {
				 countSql="select credit from orderitem where date(confirmtime)='"+dateStr+"'";
			 }
			 
		 }
		 
		 CountService countService=new CountService();
		 int creditByUserAndDay=countService.getAllCounts(countSql);
		 request.setAttribute("creditByUserAndDay",creditByUserAndDay);
		 //下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map userM=serviceTypeService.getUser();
		 request.setAttribute("user", userM);
		 return "success";
	 }
	 //按用户查看日交易次数
	 public String CountByUserAndDay(){
		 HttpServletRequest request = ServletActionContext.getRequest ();
		 //获取用户
		 int userId=-1;
		 String countSql;
		 String dateStr=request.getParameter("date");//日期格式:"yyyy-MM-dd"
		 if(request.getParameter("userId")!=null&&Integer.valueOf(request.getParameter("userId"))!=-1)
		 {    
			  userId=Integer.valueOf(request.getParameter("userId"));
			  countSql="select count(id) from orderitem where userid="+userId;
			  if(dateStr!=null&&dateStr!="")
		      {
			     countSql="select count(id) from orderitem where userid="+userId+" and date(confirmtime)='"+dateStr+"'";
		      }
			 
		 }else {
			 countSql="select count(id) from orderitem";
			 if(dateStr!=null&&dateStr!="")
		      {
				 countSql="select count(id) from orderitem where date(confirmtime)='"+dateStr+"'";
		      }
		 }
		 CountService countService=new CountService();
		 int countByUserAndDay=countService.getCounts(countSql);
		 request.setAttribute("countByUserAndDay",countByUserAndDay);
		//下拉列表的值
		 InfoService serviceTypeService=new InfoService();
		 Map userM=serviceTypeService.getUser();
		 request.setAttribute("user", userM);
		 return "success";
	 }
	 
}
